<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>admin</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-form.js"></script>
<script type="text/javascript" src="js/data.js"></script>
<link href="css/Login.css" rel="stylesheet" type="text/css" />
</head>
<script language="JavaScript">
	function searchUser() {
		document.getElementById("admin_his").style.display = 'none';
		var id = document.getElementById("inputID");
		if (id.value == null) {
			alert("请输入ID!");
			return;
		}
		$
				.ajax({
					type : "POST",
					url : "${pageContext.request.contextPath}/user/getUser",
					datatype : "json",
					data : {
						"id" : id.value
					},
					success : function(data) {
						if (data.success) {
							document.getElementById('answer').style.display = 'block';
							setUserData(data);
						} else {
							alert("用户不存在");
							document.getElementById('answer').style.display = 'none';
						}
					},
					error : function() {
						alert("网络异常");
						window.location.href = '${pageContext.request.contextPath}/redirect/login';
						return;
					}
				});
	}
	function setUserData(data) {
		var user = data.user;
		document.getElementById("userId").innerHTML = data.id;
		document.getElementById("userName").innerHTML = user.userName;
		document.getElementById("value").innerHTML = user.value;
	}
	function recharge() {
		document.getElementById("which").value = 'recharge';
		document.getElementById("options").style.display = 'none';
		document.getElementById("editNumber").style.display = 'block';
		document.getElementById("input_number").value = null;
	}
	function enchashment() {
		document.getElementById("which").value = 'enchashment';
		document.getElementById("options").style.display = 'none';
		document.getElementById("editNumber").style.display = 'block';
		document.getElementById("input_number").value = null;
	}
	function editUserValue() {
		var which = document.getElementById("which");
		var inputNumber = document.getElementById("input_number");
		var userId = document.getElementById("userId");
		if (inputNumber == null) {
			alert("请输入正确的数量");
			return;
		}
		if (inputNumber == 0) {
			alert("输入无效");
			return;
		}
		if (which.value == null) {
			alert("操作异常");
			return;
		}
		$
				.ajax({
					type : "POST",
					url : "${pageContext.request.contextPath}/user/editUserValue",
					datatype : "json",
					data : {
						"id" : userId.innerText,
						"which" : which.value,
						"inputNumber" : inputNumber.value
					},
					success : function(data) {
						if (data.success) {
							searchUser();
							cancel();
							getValue();
						} else {
							alert(data.msg);
							cancel();
						}
					},
					error : function() {
						alert("网络异常");
						window.location.href = '${pageContext.request.contextPath}/redirect/login';
						return;
					}
				});
	}
	function cancel() {
		document.getElementById("which").value = null;
		document.getElementById("options").style.display = 'block';
		document.getElementById("editNumber").style.display = 'none';
	}
	function searchHistory() {
		document.getElementById("admin_his").style.display = 'block';
		document.getElementById("answer").style.display = 'none';
		setHisHeight();
		$
				.ajax({
					type : "POST",
					url : "${pageContext.request.contextPath}/allValue/getAll",
					datatype : "json",
					success : function(data) {
						if (data.success) {
							setHisData(data.list);
						}
					},
					error : function() {
						alert("网络异常");
						window.location.href = '${pageContext.request.contextPath}/redirect/login';
						return;
					}
				});

	}
	function setHisHeight() {
		var height = document.getElementById("admin").offsetHeight;
		document.getElementById("admin_his").style.height = 0.6 * height + 'px';
	}
	function setHisData(list) {
		var html = '';
		for (var i = 0; i < list.length; i++) {
			html += '<li>' + formatDate(list[i].addTime) + ' ' + '账户：'
					+ list[i].userName + ' ' + list[i].content + ' '
					+ list[i].cost + '</li>';
		}
		document.getElementById('admin_his_li').innerHTML = html;
	}
	function getValue() {
		$
				.ajax({
					type : "POST",
					url : "${pageContext.request.contextPath}/restValue/getRestValue",
					datatype : "json",
					success : function(data) {
						if (data.success) {
							document.getElementById("restValue").innerHTML = data.value;
						}
					},
					error : function() {
						alert("网络异常");
						window.location.href = '${pageContext.request.contextPath}/redirect/login';
						return;
					}
				});
	}
	function reset() {
		var userId = document.getElementById("userId");
		$
				.ajax({
					type : "POST",
					url : "${pageContext.request.contextPath}/user/resetPassword",
					datatype : "json",
					data : {
						"id" : userId.innerText
					},
					success : function(data) {
						alert(data.msg);
					},
					error : function() {
						alert("网络异常");
						window.location.href = '${pageContext.request.contextPath}/redirect/login';
						return;
					}
				});
	}
</script>
<body>
	<div id="admin" class="center jiao">
		<div class="admin">
			<div class="rest_value">
				当前剩余总数量：<label id="restValue"></label>
				<script language="JavaScript">
					getValue();
				</script>
			</div>
			<div class="search">
				输入用户ID<input type="text" style="height:24px; font-size:20px;"
					size="10" id="inputID" />
				<button style="width:80px; font-size:18px; padding-top:1px;"
					onclick="searchUser()">查询</button>
				<button style="width:80px; font-size:18px; padding-top:1px;"
					onclick="searchHistory()">查看历史</button>
			</div>
			<div class="answer" id="answer">
				<div class="answer_left">
					<table border="1" cellspacing="0" cellpadding="0">
						<tr>
							<td>用户ID</td>
							<td><label id="userId"></label></td>
						</tr>
						<tr>
							<td>姓名</td>
							<td><label id="userName"></label></td>
						</tr>
						<tr>
							<td>余额</td>
							<td><label id="value"></label></td>
						</tr>
					</table>
				</div>
				<div class="answer_right">
					<div id="resetPassword">
						<button style="width:100px; font-size:16px;" onclick="reset()">重置密码</button>
						<br /> <br />
					</div>
					<div id="options">
						<button style="width:60px; font-size:16px;" onclick="recharge()">充值</button>
						&nbsp;&nbsp;&nbsp;&nbsp;
						<button style="width:60px; font-size:16px;"
							onclick="enchashment()">提现</button>
					</div>
					<div id="editNumber" style="display:none;">
						<button style="width:60px; font-size:16px;"
							onclick="editUserValue()">确定</button>
						&nbsp;&nbsp;&nbsp;&nbsp;
						<button style="width:60px; font-size:16px;" onclick="cancel()">取消</button>
						<br /> <br />
						<div style="font-size:18px;">
							<input type="hidden" id="which" /> 输入数量:<input type="text"
								id="input_number" style="line-height: 20px;" width="12px"
								size="3" maxlength="4"
								onkeyup='this.value=this.value.replace(/\D/gi,"")' />
						</div>
					</div>
				</div>
			</div>
			<div id="admin_his" class="admin_his" style="display:none">
				<ul id="admin_his_li">
				</ul>
			</div>
		</div>
	</div>
	<%@ include file="footer.html"%>
</body>
</html>
